(function ($) {
	var Limit = 20;
	var pullArr=[]
	var pageArr = [0,0,0] //未授予 已授予 查看结果
	var pullingArr = [false,false,false]
	var sourceArr = [[],[],[]];
	var preData
	
	//判断是查看结果还是授予学分
	var teachType = '' ;//teachCredit学分 teachResult查看结果
	
	$.init();
	$.plusReady(function () {
	    baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	//列表
	function getCreditData(index){
		if (pullingArr[index]) {
			pullArr[index].endPullUpToRefresh()
			return;
		}
		var typeFlag = 'N,Y'
		if (index == 0) {
			typeFlag = 'N'
		}else if (index == 1){
			typeFlag = 'Y'
		}
		pullingArr[index] = true
		var parr = preData['rw']+'^A,N,Y^'+typeFlag;
		var inputValue = $('#search'+(index == 2?'2':''))[0].value
		var pdict = {'parr':parr,'input':inputValue,'activeName':3,'start':pageArr[index],'limit':Limit}
		//console.log(JSON.stringify(pdict))
		var paramDict = {'method':'web.INMTeaComm:LoadDrillReleaseSubList','params':JSON.stringify(pdict),'type':'RecQuery'}
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			pullingArr[index] = false
			if (result['msgcode'] != '1') {
				mui.toast(result.msg)
				pullArr[index].endPullUpToRefresh();
				return
			}
			var dataArr = result.data
			if (pageArr[index] == 0) {
				sourceArr[index] = dataArr
				$('#UL'+index)[0].innerHTML = ''
			}else{
				sourceArr[index] = sourceArr[index].concat(dataArr)
			}
			updateUI(dataArr,index)
			if (dataArr.length < Limit) {
				pullArr[index].endPullUpToRefresh(true);
			}else{
				pullArr[index].endPullUpToRefresh();
			}
			pageArr[index] += dataArr.length;
		}, function(errorStr) {
			pullArr[index].endPullUpToRefresh();
			pullingArr[index] = false
		});
	}
	
	function updateUI(dataArr,index){
		var UL = $("#UL"+index)[0];
		var btnHtml = '<div class="baseBtn portraitCenter" id=""><img src="../dependence/teach/images/arrowR.svg"/></div>';
		if (index == 0) {
			btnHtml = '<div style="width:100%"><div class="mui-btn mui-btn-primary baseBtn">'+(index == 0?'授予学分':'更改学分')+'</div></div>';
		}
		var oldCount = UL.querySelectorAll('li').length
		for(var i = 0; i < dataArr.length; i++){
			var dict = dataArr[i]
			var html =  '<div class="textTip">姓名：</div><div class="textContent">'+dict['perName']+'</div>'+
				 '<div class="textTip">工号：</div><div class="textContent">'+dict['perNo']+'</div>'+
				 '<div class="textTip">病区：</div><div class="textContent">'+dict['wardDesc']+'</div>'+
				 '<div class="textTip">考核结果：</div><div class="textContent">'+dict['DrillResultDesc']+'</div>'+
				 '<div class="textTip">获得学分：</div><div class="textContent">'+dict['UseDrillScore']+'</div>'+btnHtml
			var newLi = document.createElement('li')
			newLi.setAttribute('class','mui-table-view-cell')
			newLi.innerHTML = html
			UL.appendChild(newLi)
			var baseBtn = newLi.querySelector('.baseBtn')
			baseBtn.id = (oldCount + i) +'-'+index
			baseBtn.addEventListener('tap',baseBtnTapped) //查看结果
		}
	}

	function baseBtnTapped(){
		var idArr = this.id.split('-')
		var col = parseInt(idArr[1])
		var row = parseInt(idArr[0])
		var dict = sourceArr[col][row]
		dict['ReleaseDesc'] = preData['ReleaseDesc']
		dict['DrillCredit'] = preData['DrillCredit']
		dict['ReleaseWay'] = preData['ReleaseWay']
		dict['CheckWork'] = preData['CheckWork']
		var type = 'look'
		if (col == 0) {
			type = 'give'
		// }else if (col == 1) {//更改学分
		// 	type = 'change'
		}
		var webview = plus.webview.create('trainResult.html', 'trainResult', {
			background: '#fff',
			statusbar: {
				background: '#fff'
			}
		},{pageType:type,sourceData:dict});
		webview.show('fade-in');
		if (type == 'give') {
			webview.addEventListener('close',function(){
				refreshMainPage()
			})
		}
	}
	
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		var self = plus.webview.currentWebview()
		teachType = self.pageType;
		preData = self.sourceData
		//console.log(JSON.stringify(preData))
		if (teachType == 'teachCredit') { //title
			$('.mui-title')[0].innerText = '学分授予'
		}else{
			$('.mui-title')[0].innerText = '学员学分'
		}
		$('.mui-content')[0].classList.add(teachType)
		if (teachType == 'teachResult') {
			var search = document.getElementById('search2');
			search.addEventListener('input',function(){
				resetRefresh(2)
			});
			mui("#resultSearch").on("tap", ".searchArea .mui-icon-clear", function() {
				if (document.getElementById('search2').value != "") {
					document.getElementById('search2').value = ""
					resetRefresh(2)
				}
			});
			var scroll2 = document.getElementById('scroll2');
			pullArr[2] = $(scroll2).pullToRefresh({
				up: {
					callback: function() {
						getCreditData(2)
					}
				}
			});
			getCreditData(2);
			return
		}
		var search = document.getElementById('search');
		search.addEventListener('input',function(){
			refreshMainPage()
		});
		mui("#slider").on("tap", ".searchArea .mui-icon-clear", function() {
			if (document.getElementById('search').value != "") {
				document.getElementById('search').value = ""
				refreshMainPage()
			}
		});
		var slider = document.getElementById('slider');
		slider.addEventListener('slide', function(event) {
			var index = event.detail.slideNumber;
			refreshMainPage(index)
			$('#sliderIndicator .mui-indicator[data-index="'+index+'"]')[0].classList.add('mui-active');
		});
		var indicators = document.querySelectorAll('#sliderIndicator .mui-indicator');
		for (var i = 0; i < indicators.length;i++) {
			indicators[i].addEventListener('tap',function(){
				var oldActive = $('#sliderIndicator .mui-indicator.mui-active')[0];
				if (oldActive) {
					if (oldActive == this) {
						return
					}
					oldActive.classList.remove('mui-active')
				}
				var newIndex = parseInt(this.getAttribute('data-index'))
				refreshMainPage(newIndex)
				this.classList.add('mui-active')
				mui('#slider').slider().gotoItem(newIndex,100)
			});
		}
		
		var scroll0 = document.getElementById('scroll0');
		pullArr[0] = $(scroll0).pullToRefresh({
			up: {
				callback: function() {
					getCreditData(0)
				}
			}
		});
		var scroll1 = document.getElementById('scroll1');
		pullArr[1] = $(scroll1).pullToRefresh({
			up: {
				callback: function() {
					getCreditData(1)
				}
			}
		});
		getCreditData(0);
		getCreditData(1)
	}
	function refreshMainPage(newIndex){
		if (teachType == 'teachResult') {
			resetRefresh(2);
			return;
		}
		if (newIndex == undefined) {
			resetRefresh(0);
			resetRefresh(1)
			return
		}
		resetRefresh(newIndex)
	}
	function resetRefresh(index){
		pageArr[index] = 0;
		sourceArr[index] = []
		if (pullArr[index]) {
			pullArr[index].refresh(true)
		}
		getCreditData(index);
		$('#wrapper'+index).scroll().scrollTo(0,0,100);
	}
})(mui);